package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sky.context.BaseContext;
import com.sky.dto.VoucherDTO;
import com.sky.entity.SeckillVoucher;
import com.sky.entity.Voucher;
import com.sky.entity.VoucherOrder;
import com.sky.mapper.VoucherMapper;
import com.sky.result.Result;
import com.sky.service.SeckillVoucherService;
import com.sky.service.VoucherService;
import com.sky.utils.RedisIdWorker;
import com.sky.vo.VoucherVO;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.stream.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
@Service
@Slf4j
public class VoucherServiceImpl implements VoucherService {
    @Autowired
    private VoucherMapper voucherMapper;

    @Autowired
    private SeckillVoucherService seckillVoucherService;

    @Override
    public void add(VoucherDTO dto) {
        Voucher voucher = new Voucher();
        BeanUtils.copyProperties(dto, voucher);
        voucherMapper.insert(voucher);

        // 保存秒杀信息
        SeckillVoucher seckillVoucher = new SeckillVoucher();
        seckillVoucher.setVoucherId(voucher.getId());
        seckillVoucher.setStock(voucher.getStock());
        seckillVoucher.setCreateTime(LocalDateTime.now());
        seckillVoucher.setBeginTime(voucher.getBeginTime());
        seckillVoucher.setEndTime(voucher.getEndTime());
        seckillVoucher.setUpdateTime(LocalDateTime.now());
        seckillVoucherService.add(seckillVoucher);
    }

    @Override
    public void update(VoucherDTO dto) {
        Voucher voucher = new Voucher();
        BeanUtils.copyProperties(dto, voucher);
        voucherMapper.update(voucher);
    }

    @Override
    public void delete(Long id) {
        voucherMapper.deleteById(id);
    }

    @Override
    public VoucherVO getById(Long id) {
        Voucher voucher = voucherMapper.getById(id);
        if (voucher == null) return null;
        VoucherVO vo = new VoucherVO();
        BeanUtils.copyProperties(voucher, vo);
        return vo;
    }



    @Override
    public List<VoucherVO> list() {
        return voucherMapper.list().stream().map(voucher -> {
            VoucherVO vo = new VoucherVO();
            BeanUtils.copyProperties(voucher, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}